﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeckoBrowser.Data;
using GeckoBrowser.Enums;
using GeckoBrowser.Utils;
using GeckoBrowser.WebBrowser;
using MediaPortal.Configuration;
using MediaPortal.GUI.Library;
using MediaPortal.Profile;

namespace GeckoBrowser.GUIControls
{
    public class WebBrowserControl: GUIControl
    {
        private bool _rendered;
        private string LinkId { get; set; }
        public WebBrowserControl(int parentId): base(parentId)
        {
            _rendered = false;
            
        }

        public override void AllocResources()
        {
            Init();
            _rendered = false;
            if (!GeckoBrowserManager.InitializationSuccess)
            {
                //GeckoBrowserUtils.ShowDialogOkWindow("Error!", "Initializing GeckoBrowser was unsuccessful. See log for details.");
            }
            base.AllocResources();
        }
        public override void FreeResources()
        {
            DeInit();
            _rendered = false;
            base.FreeResources();
        }
        private Size GetNewSize(Size oldSize)
        {
            float fSkinHeight = (float)GUIGraphicsContext.SkinSize.Height;
            float fPercentY = ((float)GUIGraphicsContext.Height) / fSkinHeight;
            int height = (int)Math.Round(oldSize.Height * fPercentY);

            float fSkinWidth = (float)GUIGraphicsContext.SkinSize.Width;
            float fPercentX = ((float)GUIGraphicsContext.Width) / fSkinWidth;
            int width = (int)Math.Round(oldSize.Width * fPercentX);
            return new Size(width, height);
        }

        public override void Render(float timePassed)
        {
            if(!IsVisible)
            {
                //GeckoBrowserManager.UnsetWebBrowserWindow();
                //_rendered = false;
            }
            else
            {
                if (!_rendered)
                {
                    if (GeckoBrowserManager.InitializationSuccess)
                    {
                        GeckoBrowserManager.SetWebBrowserWindow(new Point(_positionX, _positionY), new Size(_width, _height));
                        while (GeckoBrowserManager.InitializationInProgress)
                        {
                            System.Windows.Forms.Application.DoEvents();
                        }
                        GeckoBrowserManager.Navigate(GeckoBrowserManager.GetStartpageLink(), GeckoBrowserManager.GetDefaultZoom());
                        _rendered = true;
                    }
                }
                
            }
            base.Render(timePassed);
        }


        public override void OnDeInit()
        {
            GeckoBrowserManager.UnsetWebBrowserWindow();
            base.OnDeInit();
        }

        public void Init()
        {
            if (GeckoBrowserManager.InitWebBrowser())
            {
                using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
                {
                    GeckoBrowserManager.Startpage = settings.GetValueAsString("GeckoBrowser", "Startpage", "");
                    GeckoBrowserManager.ZoomStep = settings.GetValueAsInt("GeckoBrowser", "Zoomstep", 10);
                    GeckoBrowserManager.DefaultZoom = settings.GetValueAsInt("GeckoBrowser", "Defaultzoom", 100);
                    GeckoBrowserManager.MouselessBrowsing = settings.GetValueAsBool("GeckoBrowser", "RemoteBrowsing", false);
                }
            }
        }
        public void DeInit()
        {
            if (GeckoBrowserManager.InitializationSuccess)
            {
                GeckoBrowserManager.DeInitWebBrowser();
            }
            
        }

        /// <summary>
        /// Called when [action].
        /// </summary>
        /// <param name="action">The action.</param>
        public override void OnAction(Action action)
        {
            if (GeckoBrowserManager.InitializationSuccess)
            {
                switch (action.wID)
                {
                    case Action.ActionType.ACTION_MOUSE_MOVE:
                        {
                            GeckoBrowserManager.Focus();
                            break;
                        }
                }
            }
            base.OnAction(action);
        }
    }
}
